 <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>上传头像</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="apple-mobile-web-app-status-bar-style" content="black">
	<meta name="apple-mobile-web-app-capable" content="yes">
	<meta name="format-detection" content="telephone=no">
	<link rel="stylesheet" href="/assets/layui-v2.5.5/css/layui.css" media="all" />
</head>
<body> 
<div class="layui-fluid" style="margin-top:2%;">
		<form class="layui-form" action="" id="file_form" enctype="multipart/form-data">
				<div class="layui-form-item">
				    <label class="layui-form-label">选择图片<font color="#FF0000">*</font></label>
				    <div class="layui-input-block">
				      <input type="file" name="image" id="test1" lay-verify="required" class="layui-input">
				    </div>
				  </div>
			<div class="layui-form-item">
				<div class="layui-input-block" style="text-align: center;margin:0 auto;">
					<button class="layui-btn" lay-submit="" lay-filter="submit-teacher">保存</button> 
					<button type="reset" class="layui-btn layui-btn-primary">重置</button>
				</div>
			</div>
		</form>
		<!-- 设置隐藏按钮，以便上传文件时进行非空验证 -->
<button class="layui-btn" id="test2" style="display:none;"></button>
	</div>
<script src="/assets/layui-v2.5.5/layui.js" charset="utf-8"></script> 
<script>
var $,form,upload,id;
	id='#(id)';
	layui.use(['form','jquery','upload'], function(){
	  form = layui.form;
	  upload=layui.upload;
	  $ = layui.$;
	//提交表单
		form.on('submit(submit-teacher)', function(obj){
			$("#test2").click();
			return false;
			});
		//上传开始
		upload.render({
		    elem: '#test1'
		    ,auto: false
		    ,bindAction: '#test2'
		    ,url: '/teacher/teacherUploadImg'
		    ,accept: 'images' //图片
		    ,size:10240 //最大上传10 M
		    ,before: function(obj){
	    		this.data = {
    				id: id,
	                }
	    		layer.load(0,{shade: [0.8, '#393D49'],content: '文件上传中,请稍后...'});
	    	  }
		    ,done: function(res, index, upload){
		        layer.closeAll('loading'); //关闭loading
		        var index = parent.layer.getFrameIndex(window.name);
				parent.layer.close(index);
		        parent.$(".layui-laypage-btn").click();
		      }
		    ,error: function(){
		      //请求异常回调
		      layer.msg("上传失败！");
		    }
	  });
	//上传结束
	});
</script> 
</body>
</html>